package com.zwcloud.zw.railway.controller;

import com.zwcloud.zw.railway.service.TrainService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.HttpServerErrorException;

@RestController
public class TrainController {

    @Resource
    private TrainService trainService;

    // 调用 addFreightYardDivision 接口
    @GetMapping("/api/train/callFreightYardApi")
    public String callFreightYardApi() {
        try {
            ResponseEntity<String> response = trainService.callAddFreightYardApi();
            System.out.println("响应状态: " + response.getStatusCode());
            System.out.println("响应体: " + response.getBody());
            return "API调用成功！";
        } catch (HttpClientErrorException | HttpServerErrorException e) {
            System.err.println("请求失败: " + e.getMessage());
            return "请求失败: " + e.getMessage();
        } catch (Exception e) {
            System.err.println("发生异常: " + e.getMessage());
            return "发生异常: " + e.getMessage();
        }
    }

    // 调用今天+昨天数据上传
    @GetMapping("/api/train/checkAndCall")
    public ResponseEntity<String> triggerCheckAndCallApi() {
        try {
            trainService.checkFoDataAndCallApi();
            return ResponseEntity.ok("【今天+昨天】定时任务已成功执行！");
        } catch (Exception e) {
            return ResponseEntity.status(500).body("发生异常: " + e.getMessage());
        }
    }

    // 调用运往地接口
    @GetMapping("/api/train/checkNewData")
    public ResponseEntity<String> checkNewDataAndCallApi() {
        try {
            trainService.checkForNewDataAndCallApi();
            return ResponseEntity.ok("运往地API调用成功！");
        } catch (Exception e) {
            return ResponseEntity.status(500).body("发生异常: " + e.getMessage());
        }
    }

    //  手动触发【最近两个月】上传接口 → 供 Apifox 测试
    @GetMapping("/api/train/checkTwoMonthData")
    public ResponseEntity<String> checkTwoMonthDataAndCallApi() {
        try {
            trainService.checkTwoMonthDataAndCallApi();
            return ResponseEntity.ok("【最近两个月】数据上传任务执行成功！");
        } catch (Exception e) {
            return ResponseEntity.status(500).body("发生异常: " + e.getMessage());
        }
    }

}
